local zongheng = fk.CreateSkill {
  name = "zongheng",
}

Fk:loadTranslationTable{
  ["zongheng"] = "纵横",
  [":zongheng"] = "准备阶段，你可以观看两名其他角色的手牌，你展示并获得其中一名角色的一张手牌，然后可以弃置另一名角色的手牌中与此牌"..
  "类别、花色、点数相同的牌各一张。",

  ["#zongheng-choose"] = "纵横：观看两名角色的手牌，获得其中一张，然后弃置另一名角色类别、花色、点数相同的手牌各一张",
  ["#zongheng-prey"] = "纵横：获得其中一张牌",
  ["#zongheng-discard"] = "纵横：你可以弃置其中与%arg类别、花色、点数相同的牌各一张",
}

Fk:addPoxiMethod{
  name = "zongheng_prey",
  prompt = "#zongheng-prey",
  card_filter = function (to_select, selected, data, extra_data)
    return #selected == 0
  end,
  feasible = function(selected, data)
    return #selected == 1
  end,
  default_choice = function (data, extra_data)
    return {data[1][2][1]}
  end,
}

local function zonghengJudge(id, info)
  local card, ret = Fk:getCardById(id), {}
  local info2 = {card.type, card.suit, card.number}
  for i = 1, 3 do
    if info[i] == info2[i] then
      table.insert(ret, i)
    end
  end
  return ret
end

Fk:addPoxiMethod{
  name = "zongheng_discard",
  prompt = function (data, extra_data)
    return "#zongheng-discard:::"..extra_data.prompt
  end,
  card_filter = function (to_select, selected, data, extra_data)
    if #selected < 3 and #zonghengJudge(to_select, extra_data.info) > 0 then
      local all_infos = zonghengJudge(to_select, extra_data.info)
      for _, id in ipairs(selected) do
        for _, i in ipairs(zonghengJudge(id, extra_data.info)) do
          table.insertIfNeed(all_infos, i)
        end
      end
      return #all_infos > #selected
    end
  end,
  feasible = Util.TrueFunc,
}

zongheng:addEffect(fk.EventPhaseStart, {
  anim_type = "control",
  can_trigger = function(self, event, target, player, data)
    return player == target and player:hasSkill(zongheng.name) and player.phase == Player.Start and
      #table.filter(player.room:getOtherPlayers(player, false), function (p)
        return not p:isKongcheng()
      end) > 1
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    local targets = table.filter(room:getOtherPlayers(player, false), function (p)
        return not p:isKongcheng()
      end)
    local tos = room:askToChoosePlayers(player, {
      targets = targets,
      min_num = 2,
      max_num = 2,
      prompt = "#zongheng-choose",
      skill_name = zongheng.name,
      cancelable = true,
    })
    if #tos > 0 then
      room:sortByAction(tos)
      event:setCostData(self, {tos = tos})
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local tos = event:getCostData(self).tos
    local id = room:askToPoxi(player, {
      poxi_type = "zongheng_prey",
      data = {
        { tos[1].general, tos[1]:getCardIds("h") },
        { tos[2].general, tos[2]:getCardIds("h") },
      },
      cancelable = false,
    })[1]
    local to = table.contains(tos[1]:getCardIds("h"), id) and tos[2] or tos[1]
    local info = {Fk:getCardById(id).type, Fk:getCardById(id).suit, Fk:getCardById(id).number}
    local prompt = Fk:getCardById(id):toLogString()
    room:moveCardTo(id, Card.PlayerHand, player, fk.ReasonPrey, zongheng.name, nil, true, player)
    if player.dead or not table.contains(player:getCardIds("h"), id) then return end
    room:showCards(id)
    if player.dead or to.dead or to:isKongcheng() then return end
    local result = room:askToPoxi(player, {
      poxi_type = "zongheng_discard",
      data = {
        { to.general, to:getCardIds("h") },
      },
      extra_data = {
        info = info,
        prompt = prompt,
      }
    })
    if #result > 0 then
      room:throwCard(result, zongheng.name, to, player)
    end
  end,
})

return zongheng
